package uit.cinemainfo;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

public class ComingActivity extends Activity {
	FilmListViewAdapter adapter;
	ListView listViewHot;
	//public ProgressDialog pd;
	DBAdapter db;
	Film[] film;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_coming);
		
		db = new DBAdapter(this);
		try{
		db.open();
        Cursor p = db.getAllComing();
        film = new Film[p.getCount()];
        int i=0;
        if(p.moveToFirst()){
        	do{
        		film[i] = new Film(p.getString(0), p.getString(1), p.getInt(2), p.getString(3),p.getString(4),p.getString(5),p.getFloat(6),p.getString(7),p.getInt(8),p.getInt(9),p.getString(10),p.getInt(11));
        		i++;
        	}while (p.moveToNext());
        }
        db.close();
		}catch(Exception ex){
			Log.w("Here", ex.getMessage());
		}
        
        adapter = new FilmListViewAdapter(getApplicationContext(), R.layout.film_listview_item, film);
        listViewHot = (ListView)findViewById(R.id.listComingFilm);
        listViewHot.setAdapter(adapter);
        listViewHot.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        	@Override
        	public void onItemClick(AdapterView av, View v, int position, long id){
        		StartDetail(position);
        	}
		});
	}

	
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_coming, menu);
		return true;
	}
	
	public boolean onPrepareOptionsMenu(Menu menu){
		MenuItem item = menu.findItem(R.id.menu_coming);
		item.setEnabled(false);
		return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
	    // Handle item selection
	    switch (item.getItemId()) {
	        case R.id.menu_showing:
	        	StartShowing();
	            return true;
	        case R.id.menu_theater:
	        	StartTheater();
	        	return true;
	        case R.id.menu_news:
	        	StartNews();
	        	return true;
	        default:
	            return super.onOptionsItemSelected(item);
	    }
	}
	
	public void StartDetail(int position){
		Intent intent = new Intent(ComingActivity.this, DetailFilmActivity.class);
		intent.putExtra("nofilm", adapter.getItem(position).nofilm);
		startActivity(intent);
	}
	
	public void StartShowing(){
		Intent intent = new Intent(ComingActivity.this, ShowingActivity.class);
		startActivity(intent);
	}
	
	public void StartTheater(){
		Intent intent = new Intent(ComingActivity.this, TheaterActivity.class);
		startActivity(intent);
	}
	public void StartNews(){
		Intent intent = new Intent(ComingActivity.this, ListNewsActivity.class);
		startActivity(intent);
	}
}
